package com.example.carrierapp.ui.main.tender.yes;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.carrierapp.base.MVPBaseFragment;
import com.example.carrierapp.bean.tender.TenderListBean;
import com.example.carrierapp.ui.adapter.TenderNoAdatpter;
import com.example.carrierapp.ui.view.recycleview.HorizontalDividerDecoration;
import com.example.carrierapp.ui.view.recycleview.WrapContentManager;
import com.example.carrierapp.ui.view.recycleview.XRecyclerView;
import com.example.carrierapp.util.ConnectionUtil;
import com.example.carrierapp.util.DisplayUtil;
import com.example.carrierapp.util.LogUtil;
import com.example.carrierapp.util.ToastUtils;
import com.example.lb.fingerdemo.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 已中标
 */
public class TenderYesFragment  extends MVPBaseFragment<TenderYesContract.View,TenderYesPresenter> implements TenderYesContract.View ,XRecyclerView.LoadingListener{
    @BindView(R.id.ll_empty)
    LinearLayout mEmptyView;
    @BindView(R.id.recycler_view)
    XRecyclerView mRecyclerView;
    private TenderNoAdatpter mAdapter;
    private int mPage = 0;
    private boolean mLoadMore;

    public static TenderYesFragment newInstance() {
        TenderYesFragment homeFragment = new TenderYesFragment();
        return homeFragment;
    }
    @Override
    protected int getLayout() {
        return R.layout.fragment_tender_yes;
    }

    @Override
    protected TenderYesPresenter createPresenter() {
        return new TenderYesPresenter(getContext());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this,mRootView);
        initRecycleView();
    }

    @Override
    public void initData() {
        super.initData();
        showProgress();
        mPresenter.attachView(this);
        mPresenter.tenderList(true,mPage);
    }
    private void initRecycleView() {
        WrapContentManager mLinearLayoutManager = new WrapContentManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.addItemDecoration(new HorizontalDividerDecoration(getContext(), R.drawable.divider_horizontal_10_f3f6fa, DisplayUtil.dip2px(getContext(), 10)));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLoadingMoreEnabled(true);
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadingListener(this);
        mRecyclerView.refreshComplete();
        mRecyclerView.loadMoreComplete();

        mAdapter = new TenderNoAdatpter(true);
        mRecyclerView.setAdapter(mAdapter);
    }
    @Override
    public void tenderListSuccess(TenderListBean bean) {
        dismissProgress();
        LogUtil.d("TenderNoFragment-getDispatchListSuccess-" + bean);
        if (mRecyclerView != null) {
            mRecyclerView.loadMoreComplete();
            mRecyclerView.refreshComplete();
        }
        List<TenderListBean.TenderListInfo> list = bean.getItems();
        LogUtil.d("TenderNoFragment-getWaybillListSuccess-" + list.size());
        mEmptyView.setVisibility(View.GONE);
        if (!ConnectionUtil.isNull(list)) {
            if (mLoadMore) {
                mLoadMore = false;
                mAdapter.addDataNotClear(list);
            } else {
                mAdapter.addData(list);
            }
            if (mRecyclerView != null && list.size() < 10) {
                mRecyclerView.setNoMore(true);
            } else {
                mRecyclerView.setNoMore(false);
            }
        } else {
            mRecyclerView.setNoMore(true);
            if (!mLoadMore) {
                mAdapter.clear();
                mEmptyView.setVisibility(View.VISIBLE);
            } else {
                mEmptyView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void tenderListFailde(String msg) {
        dismissProgress();
        LogUtil.d("TenderNoFragment-tenderListFailde-" + msg);
        ToastUtils.showCenterToast(msg, getContext());
        if (mRecyclerView != null) {
            mRecyclerView.loadMoreComplete();
            mRecyclerView.refreshComplete();
        }
        mEmptyView.setVisibility(mAdapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onRefresh() {
        mPage = 0;
        mPresenter.tenderList(true,mPage);
        mLoadMore = false;
    }

    @Override
    public void onLoadMore() {
        mPage++;
        mPresenter.tenderList(true,mPage);
        mLoadMore = true;
    }
}
